package com.xt.myapplication;

import androidx.annotation.RequiresApi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xt.pro.BleNFCBindInfo;
import com.xthh._function.HH_FileUtil;
import com.xthh.esl.BmpUtils;
import com.xthh.esl.BroadcastManager;
import com.xthh.esl.CommandNFC;
import com.xthh.esl.DeviceInfo;
import com.xthh.esl.MyThread;
import com.xthh.esl.NfcConstant;
import com.xthh.esl.UIUtils;
import com.xthh.esl.XTBaseNFCActivity_ESL;
import com.xthh.XTCore;
import com.xthh.XTUI;
import com.xthh._ble.HH_BleCommunicator;
import com.xthh._enum.HH_EnumErrorCode;
import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._enum.HH_EnumFileType;
import com.xthh._enum.HH_EnumNfcErrorCode;
import com.xthh._enum.pic_storage.HH_EnumPicStorage_BrushType;
import com.xthh._enum.pic_storage.HH_EnumPicStorage_PicIndex;
import com.xthh._function.HH_CharUtil;
import com.xthh._function.HH_DialogUtil;
import com.xthh._function.HH_Logging;
import com.xthh._function.HH_PicUtils;
import com.xthh._function.HH_Spinner;
import com.xthh._function.HH_SystemUtil;
import com.xthh._function.HH_TimeUtil;
import com.xthh._function.HH_ToastUtils;
import com.xthh._function.MyPopWindow3.HH_T_SpinnerRosInfo;
import com.xthh._function._ini.HH_INI;
import com.xthh._function._language.HH_LanguageUtil;
import com.xthh._msg.HH_MSG;
import com.xthh._struct.HH_T_BindDevice;
import com.xthh._struct.HH_T_RGB_TJ;
import com.xthh._struct.HH_T_ScanResult;
import com.xthh._struct.HH_T_SystemParams;
import com.xthh._struct.HH_T_SNInfo;
import com.xthh.picselect.HH_Activity_PicFileSelect;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class MainActivity extends XTBaseNFCActivity_ESL implements View.OnClickListener {
    final String TAG = "MainActivity";



    //private static DeviceInfo deviceInfo;
    //private static Handler mHandler;
    //public ReceiveHandler receiveHandler;


    Context context;
    Button button_ota;
    Button button_ver_lib;
    Button button_ver_hard;
    Button button_scan;
    Button button_brushpic_1;
    Button button_brushpic_2;
    Button button_brushpic_3;
    Button button_brushpic_cancel;
    Button button_nfc_close;
    Button button_identity;
    Button button_close_identity;
    Button button_nfc_unlock;
    Button button_nfc_bind;
    Button button_dither16;
    Button button_pic_up;
    Button button_pic_lb;
    Button button_otp;
    ImageView imageview_pic_1;
    ImageView imageview_pic_2;
    ImageView imageview_pic_3;

    TextView textView_w;
    TextView textView_h;
    TextView textView_color;
    TextView textView_mac;
    ProgressBar progressba_brushpic;

    String strEslPath = null;

    //String testMac = "A79260A24E1C";
    //String testMac = "38360F017DD4";
    String testMac = "38360F017B35";

    final int  epdParams = -2; // EPD驱动参数，1字节，（-1时自动设置，-2保持不变，其它手动设置）
    // 定长一定是10字节
    // 正确的
    final String strDeviceIdentity = "XTHH250625"; // 这个不能修改，否则会引起标签无法使用
    // 错误的


    //final String strDeviceIdentity = ""; // 这个不能修改，否则会引起标签无法使用

    int popWindowSel = 2;

    public static HandlenRecvMessage handlenRecvMessage;

    TextView button_0;
    TextView button_1;
    TextView button_2;
    TextView button_3;
    TextView button_4;
    TextView button_5;
    TextView button_6;
    TextView button_7;
    TextView button_8;
    TextView button_9;
    TextView button_A;
    TextView button_B;
    TextView button_C;
    TextView button_D;
    TextView button_E;
    TextView button_F;
    TextView button_DEL;
    TextView button_DELALL;
    int deviceW = 800;
    int deviceH = 480;
    HH_EnumDeviceColorType deviceColorType = HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;
    List<Integer> listSendData1 = new ArrayList<>();
    List<Integer> listSendData2 = new ArrayList<>();
    List<Integer> listSendData3 = new ArrayList<>();
    HH_T_SystemParams systemParams;

    private AdapterCurrentTaskLogInfo mAdapterCurrentTaskLogInfo;
    ListView list_log_info;


    Button button_popwindow;
    Button button_popwindow2;
    Button button_popwindow3;
    Button button_popwindow44;
    Button button_battery;
    Button button_del_all_pic;
    Button button_pic_test;
    Button button_esl_brush;


    EditText edittext_page_intervaltime;
    int nPageIntervalTime = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        //receiveHandler = new ReceiveHandler(this);

        HH_LanguageUtil.SetCurrentActivityLanguage(context);
        //HH_SpUserUtils.putString(context, "language", HH_LanguageUtil.ENGLISH);


        progressba_brushpic = findViewById(R.id.progressba_brushpic);

        imageview_pic_1 = findViewById(R.id.imageview_pic_1);
        imageview_pic_1.setOnClickListener(this);
        imageview_pic_2 = findViewById(R.id.imageview_pic_2);
        imageview_pic_2.setOnClickListener(this);
        imageview_pic_3 = findViewById(R.id.imageview_pic_3);
        imageview_pic_3.setOnClickListener(this);
        button_otp = findViewById(R.id.button_otp);
        button_otp.setOnClickListener(this);
        button_esl_brush = findViewById(R.id.button_esl_brush);
        button_esl_brush.setOnClickListener(this);

        button_nfc_bind = findViewById(R.id.button_nfc_bind);
        button_nfc_bind.setOnClickListener(this);
        button_ver_lib = findViewById(R.id.button_ver_lib);
        button_ver_lib.setOnClickListener(this);
        button_dither16 = findViewById(R.id.button_dither16);
        button_dither16.setOnClickListener(this);
        button_battery = findViewById(R.id.button_battery);
        button_battery.setOnClickListener(this);

        button_nfc_unlock = findViewById(R.id.button_nfc_unlock);
        button_nfc_unlock.setOnClickListener(this);
        button_ver_hard = findViewById(R.id.button_ver_hard);
        button_ver_hard.setOnClickListener(this);

        button_brushpic_cancel = findViewById(R.id.button_brushpic_cancel);
        button_brushpic_cancel.setOnClickListener(this);
        button_pic_up = findViewById(R.id.button_pic_up);
        button_pic_up.setOnClickListener(this);
        button_pic_lb = findViewById(R.id.button_pic_lb);
        button_pic_lb.setOnClickListener(this);


        button_brushpic_1 = findViewById(R.id.button_brushpic_1);
        button_brushpic_1.setOnClickListener(this);
        button_brushpic_2 = findViewById(R.id.button_brushpic_2);
        button_brushpic_2.setOnClickListener(this);
        button_brushpic_3 = findViewById(R.id.button_brushpic_3);
        button_brushpic_3.setOnClickListener(this);


        button_identity = findViewById(R.id.button_identity);
        button_identity.setOnClickListener(this);

        button_close_identity = findViewById(R.id.button_close_identity);
        button_close_identity.setOnClickListener(this);

        button_ota = findViewById(R.id.button_ota);
        button_ota.setOnClickListener(this);
        button_pic_test = findViewById(R.id.button_pic_test);
        button_pic_test.setOnClickListener(this);

        button_nfc_close = findViewById(R.id.button_nfc_close);
        button_nfc_close.setOnClickListener(this);

        button_popwindow = findViewById(R.id.button_popwindow);
        button_popwindow.setOnClickListener(this);
        button_popwindow2 = findViewById(R.id.button_popwindow2);
        button_popwindow2.setOnClickListener(this);
        button_popwindow3 = findViewById(R.id.button_popwindow3);
        button_popwindow3.setOnClickListener(this);
        button_popwindow44 = findViewById(R.id.button_popwindow44);
        button_popwindow44.setOnClickListener(this);
        button_del_all_pic = findViewById(R.id.button_del_all_pic);
        button_del_all_pic.setOnClickListener(this);

        textView_w = findViewById(R.id.textView_w);
        textView_h = findViewById(R.id.textView_h);
        textView_color = findViewById(R.id.textView_color);
        textView_mac = findViewById(R.id.textView_mac);

        button_scan = findViewById(R.id.button_scan);
        button_scan.setOnClickListener(this);

        button_0 = findViewById(R.id.button_0);
        button_1 = findViewById(R.id.button_1);
        button_2 = findViewById(R.id.button_2);
        button_3 = findViewById(R.id.button_3);
        button_4 = findViewById(R.id.button_4);
        button_5 = findViewById(R.id.button_5);
        button_6 = findViewById(R.id.button_6);
        button_7 = findViewById(R.id.button_7);
        button_8 = findViewById(R.id.button_8);
        button_9 = findViewById(R.id.button_9);
        button_A = findViewById(R.id.button_A);
        button_B = findViewById(R.id.button_B);
        button_C = findViewById(R.id.button_C);
        button_D = findViewById(R.id.button_D);
        button_E = findViewById(R.id.button_E);
        button_F = findViewById(R.id.button_F);
        button_DEL = findViewById(R.id.button_DEL);
        button_DELALL = findViewById(R.id.button_DELALL);

        button_0.setOnClickListener(this);
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);
        button_4.setOnClickListener(this);
        button_5.setOnClickListener(this);
        button_6.setOnClickListener(this);
        button_7.setOnClickListener(this);
        button_8.setOnClickListener(this);
        button_9.setOnClickListener(this);
        button_A.setOnClickListener(this);
        button_B.setOnClickListener(this);
        button_C.setOnClickListener(this);
        button_D.setOnClickListener(this);
        button_E.setOnClickListener(this);
        button_F.setOnClickListener(this);
        button_DEL.setOnClickListener(this);
        button_DELALL.setOnClickListener(this);


        handlenRecvMessage = new HandlenRecvMessage();

        handlerBaseNfc = handlenRecvMessage;


        list_log_info = findViewById(R.id.list_log_info);
        mAdapterCurrentTaskLogInfo = new AdapterCurrentTaskLogInfo(this);
        list_log_info.setAdapter(mAdapterCurrentTaskLogInfo);

        systemParams = new HH_T_SystemParams(getApplication(), context, handlenRecvMessage);

        deviceW = HH_INI.getInt(systemParams, "DEVICE_W");
        deviceH = HH_INI.getInt(systemParams, "DEVICE_H");
        testMac = HH_INI.getString(systemParams, "DEVICE_MAC");

        //HH_INI.putString(systemParams, "DEVICE_MAC", "ADE1A7A24E1C");
        deviceColorType = HH_INI.getDeviceColor(systemParams, "DEVICE_COLOR");
        //HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3);
        // 测试16色抖点
        //deviceColorType = HH_EnumDeviceColorType.N_COLOR_16_6;

        deviceW = 528;
        deviceH = 768;
        testMac = "ADE1A7A24E1C";
        deviceColorType = HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;


        edittext_page_intervaltime = findViewById(R.id.edittext_page_intervaltime);
        int t = HH_INI.getInt(systemParams, "PAGE_BIG_INTERVAL_TIME");
        if (t < 10){
            t = 10;
            HH_INI.putInt(systemParams, "PAGE_BIG_INTERVAL_TIME", t);
        }
        nPageIntervalTime = t;
        edittext_page_intervaltime.setText(String.format("%d", t));
        edittext_page_intervaltime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && s.toString() != null){
                    int t = HH_CharUtil.Atoi(s.toString());
                    HH_INI.putInt(systemParams, "PAGE_BIG_INTERVAL_TIME", t);
                    nPageIntervalTime = t;
                }
            }
        });


        if (deviceW == 0 || deviceH == 0){
            deviceW = 528;
            deviceH = 768;
            testMac = "31391801D776";
            deviceColorType = HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;

            HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
            HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
            HH_INI.putString(systemParams, "DEVICE_MAC", testMac);
            HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);
        }


        textView_w.setText(String.format("%d", deviceW));
        textView_h.setText(String.format("%d", deviceH));
        textView_mac.setText(testMac);
        textView_color.setText(String.format("%d", deviceColorType.ordinal()));


        // 设置禁止在调用打印日志时自动写文件的功能
        XTCore.SetLogToFile(false);

        mAdapterCurrentTaskLogInfo.addTaskInfo(getString(R.string.strID_371));
        String s = HH_SystemUtil.GetAppRootSaveFolder(getApplication(), "template");


        HH_SystemUtil.OpenPerm(this, new HH_SystemUtil.ResultCallBackPerm() {
            @Override
            public void callback() {
                initApp();
            }
        });
    }



    /*private void OpenPerm() {
        PermissionX.init(this)
                .permissions(
                        Manifest.permission.BLUETOOTH,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        //Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .explainReasonBeforeRequest()
                .onExplainRequestReason((scope, deniedList, beforeRequest) -> scope.showRequestReasonDialog(deniedList,
                        getString(R.string.strID_548),
                        getString(R.string.strID_181),
                        getString(R.string.strID_384)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
                        if (allGranted) {
                            initApp();
                        } else {
                            //requestPermission();
                        }
                    }
                });
    }*/

    private void initApp() {


    }


    private void LoadLocalPic(String src) {
        Bitmap b1 = HH_PicUtils.GetBitmapFormFile(src);
        if (b1 != null) {

            if (b1.getWidth() != deviceW || b1.getHeight() != deviceH) {

                // 强制拉伸
                //b1 = HH_PicUtils.scaleBitmap(b1, deviceW, deviceH);

                // 固定比例缩放图片，其它补色
                b1 = HH_PicUtils.zoomImg2(b1, deviceW, deviceH, 255, 255, 255);

            }
            if (b1 != null) {

                // 测试的图需要旋转
                b1 = HH_PicUtils.rotateBitmap(b1, 270);

                // 转16位
                //b1 = HH_PicUtils.convertBitmapTo16BitBMP(b1);


                // 压缩
                //ByteArrayOutputStream out = new ByteArrayOutputStream();
                //b1.compress(Bitmap.CompressFormat.JPEG, 30, out);

                String tempPath1 = YS(b1, 5, 13);
                String tempPath2 = YS(b1, 8, 14);
                String tempPath3 = YS(b1, 10, 15);






                /*String tempPath1 = HH_SystemUtil.GetTempPath(systemParams,10, HH_EnumFileType.BMP);
                String tempPath2 = HH_SystemUtil.GetTempPath(systemParams,11, HH_EnumFileType.BMP);
                String tempPath3 = HH_SystemUtil.GetTempPath(systemParams,12, HH_EnumFileType.BMP);
                HH_PicUtils.BitmapSaveFile(b1, tempPath1);
                HH_PicUtils.BitmapSaveFile(b1, tempPath2);
                HH_PicUtils.BitmapSaveFile(b1, tempPath3);*/



                listSendData1 = new ArrayList<>();
                listSendData2 = new ArrayList<>();
                listSendData3 = new ArrayList<>();
                listSendData1 = DitherAddToControl(0, tempPath1, imageview_pic_1, 7);
                listSendData2 = DitherAddToControl(1, tempPath2, imageview_pic_2, 7);
                listSendData3 = DitherAddToControl(2, tempPath3, imageview_pic_3, 7);

                HH_Logging.i(TAG, "len = %d,%d,%d", listSendData1.size(), listSendData2.size(), listSendData3.size());

                // 抖点测试
                /*for (int i = 2; i <= 57; i++){
                    Bitmap b2 = HH_PicUtils.GetBitmapFormFile(src);
                    b2 = XTCore.PicDither(b2, deviceColorType, i);
                    if (b2 != null){
                        HH_Logging.i(TAG, "success=%d", i);
                        String tempPath = HH_SystemUtil.GetTempPath(systemParams, 100 + i, HH_EnumFileType.PNG);
                        HH_PicUtils.BitmapSaveFile(b2, tempPath);
                    } else {
                        HH_Logging.i(TAG, "fail=%d", i);
                    }
                }*/
                return;

            }
        }
    }

    private String YS(Bitmap b1, int compressValue, int fileIndex) {
        final CountDownLatch latch1 = new CountDownLatch(1);
        HH_Logging.i(TAG, "com begin");
        String tempPath1 = HH_SystemUtil.GetTempPath(systemParams,fileIndex, HH_EnumFileType.JPG);
        HH_PicUtils.CompressImageByQuality(b1, tempPath1, compressValue, new HH_PicUtils.ResultCallBackWrite() {
            @Override
            public void callback(String writeResult) {
                HH_Logging.i(TAG, "com callback path = %s", writeResult);

                latch1.countDown();
            }
        });


        try {
            latch1.await();
        } catch (InterruptedException e) {
        }

        return tempPath1;
    }

    private List<Integer> DitherAddToControl(int ditherType, String path, ImageView view, int pointCount) {

        Bitmap b1 = HH_PicUtils.GetBitmapFormFile(path);

        Long timeBegin = HH_TimeUtil.MadeMillisTimeMS();

        //b1 = XTCore.PicDither(b1, deviceColorType, ditherType, pointCount);
        b1 = XTCore.PicDither(b1, deviceColorType, ditherType != 0, ditherType);



        HH_Logging.i(TAG, systemParams, "抖点耗时：%d,分辨率=%d*%d", HH_TimeUtil.MadeMillisTimeMS() - timeBegin, b1.getWidth(), b1.getHeight());

        // 颜色统计
        List<HH_T_RGB_TJ> colorInfo2 = XTCore.GetPicColorTypeCount(b1);
        if (colorInfo2 != null){
            HH_Logging.i(TAG, "颜色个数=%d,颜色=%s", colorInfo2.size(), deviceColorType.toString());
            for (int i = 0; i < colorInfo2.size(); i++){
                HH_Logging.i(TAG, "RGB=%d,%d,%d", colorInfo2.get(i).getR(), colorInfo2.get(i).getG(), colorInfo2.get(i).getB(), colorInfo2.get(i).getCount());
            }
        }

        if (b1 != null) {
            // 临时写入文件
            String tempPath = HH_SystemUtil.GetTempPath(systemParams, ditherType, HH_EnumFileType.BMP);
            HH_PicUtils.BitmapSaveFile(b1, tempPath);
            Bitmap b2 = HH_PicUtils.GetBitmapFormFile(tempPath);
            b2 = HH_PicUtils.rotateBitmap(b2, 90);


            // 以下是复旦微用的
            // 需要用复旦微的图片
            strEslPath =  HH_SystemUtil.GetTempPath(systemParams, 97 + ditherType, HH_EnumFileType.BMP);
            BmpUtils.saveBmp(b2, strEslPath);



            view.setImageBitmap(b2);
        }

        return  XTCore.GetSendPicData(b1, deviceColorType);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_otp:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = XTCore.LedControl(systemParams, testMac, "", 1, 1, 1);
                            HH_Logging.i(TAG, systemParams, "XTCore.LedControl = %s", code.toString());
                        } catch (Exception e) {
                            HH_Logging.i(TAG, systemParams, "LedControl-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_pic_test:{
                Intent intent = new Intent(context, PicDisplayActivity.class);
                intent.putExtra("MAC", testMac);
                startActivity(intent);
                break;
            }
            case R.id.button_pic_lb:{
                if (listSendData2== null || listSendData2.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }

                List<String> mListType = new ArrayList<>();
                for (int i = 1; i <= 6; i++)
                    mListType.add(String.format("%03d", i));
                XTUI.Spinner4(systemParams, mListType, button_pic_lb, button_pic_lb.getWidth(), 300, new HH_Spinner.ResultCallBack() {
                    @Override
                    public void callback(int pos, String value) {
                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    HH_EnumPicStorage_PicIndex picIndex = HH_EnumPicStorage_PicIndex.values()[pos + 1];

                                    HH_EnumErrorCode code = XTCore.ImageStorage(systemParams, testMac, listSendData2,
                                            nPageIntervalTime, strDeviceIdentity,
                                            picIndex,
                                            HH_EnumPicStorage_BrushType.LOOP_PLAY,
                                            1, null);
                                    HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                                } catch (Exception e) {
                                    HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                                    e.printStackTrace();
                                }
                            }
                        }).start();

                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {

                    }
                });
                break;
            }
            case R.id.button_pic_up:{
                if (listSendData2== null || listSendData2.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }

                List<String> mListType = new ArrayList<>();
                for (int i = 1; i <= 6; i++)
                    mListType.add(String.format("%03d", i));
                XTUI.Spinner4(systemParams, mListType, button_pic_up, button_pic_up.getWidth(), 300, new HH_Spinner.ResultCallBack() {
                    @Override
                    public void callback(int pos, String value) {

                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    HH_EnumPicStorage_PicIndex picIndex = HH_EnumPicStorage_PicIndex.values()[pos + 1];

                                    HH_EnumErrorCode code = XTCore.ImageStorage(systemParams, testMac, listSendData2,
                                            nPageIntervalTime, strDeviceIdentity,
                                            picIndex,
                                            HH_EnumPicStorage_BrushType.SAVE_AND_BRUSH,
                                            0, null);
                                    HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                                } catch (Exception e) {
                                    HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                                    e.printStackTrace();
                                }
                            }
                        }).start();

                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {

                    }
                });
                break;
            }
            case R.id.button_battery:{
                int battery = XTCore.GetBatteryPercentage(context);

                mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("电量百分比：%d", battery));
                mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                    list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);

                break;
            }
            case R.id.button_nfc_bind:{
                //GetNFCCardInfo(systemParams, true);

                // 复旦微的绑定方式
                NFCCommand(new HH_T_SystemParams(getApplication(), context, handlenRecvMessage), CommandNFC.BIND, MSG_NFC_STATE, "", null);
                //showNfcDialog(context);

                break;
            }
            case R.id.button_esl_brush:{

                if (!HH_FileUtil.IsExists(strEslPath)){
                    HH_DialogUtil.tipsDialog(this, "文件不存在");
                    break;
                }


                // 复旦微图片


                DeviceInfo diBindINfo = new DeviceInfo();
                diBindINfo.setUID(testMac);
                diBindINfo.setColor(HH_SystemUtil.ColorChange_XT_To_ESL(deviceColorType));
                diBindINfo.setWidth(deviceW);
                diBindINfo.setHeight(deviceH);
                //di.setco
                // 复旦微刷图
                NFCCommand(new HH_T_SystemParams(getApplication(), context, handlenRecvMessage), CommandNFC.PIC, MSG_NFC_STATE, strEslPath, diBindINfo);
                //showNfcDialog(context);

                break;
            }
            case R.id.button_dither16:{
                Intent intent = new Intent(this, HH_Activity_PicFileSelect.class);
                ArrayList<String> listExts = new ArrayList<String>();
                listExts.add("png");
                listExts.add("jpg");
                listExts.add("jpeg");
                listExts.add("bmp");
                intent.putStringArrayListExtra(HH_Activity_PicFileSelect.FIND_FILE_EXT, listExts);
                intent.putExtra(HH_Activity_PicFileSelect.GET_FOLDER_DETAILS_INFO, false);
                startActivityForResult(intent, HH_MSG.XT_FILE_SELECT_PIC);
                break;
            }
            case R.id.button_popwindow:{
                List<String> mListType = new ArrayList<>();
                for (int i = 0; i < 11; i++)
                    mListType.add(String.format("%03d", i));
                XTUI.Spinner(systemParams, mListType, button_popwindow, button_popwindow.getWidth(), 500, 0, new HH_Spinner.ResultCallBack() {
                    @Override
                    public void callback(int pos, String value) {
                        mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("%d-%s", pos, value));
                        mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                        if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                            list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {
                        HH_Logging.i(TAG, "ResultDisBack");
                    }
                });
                break;
            }
            case R.id.button_popwindow44:{
                List<String> mListType = new ArrayList<>();
                for (int i = 0; i < 11; i++)
                    mListType.add(String.format("%03d", i));
                XTUI.Spinner4(systemParams, mListType, button_popwindow44, button_popwindow44.getWidth(), 300, new HH_Spinner.ResultCallBack() {
                    @Override
                    public void callback(int pos, String value) {
                        mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("%d-%s", pos, value));
                        mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                        if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                            list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {
                        HH_Logging.i(TAG, "ResultDisBack");
                    }
                });
                break;
            }
            case R.id.button_popwindow2:{
                List<String> mListType = new ArrayList<>();
                for (int i = 0; i < 1; i++)
                    mListType.add(String.format("%03d", i));
                XTUI.Spinner2(systemParams, mListType, button_popwindow2, button_popwindow2.getWidth(), 500, 0, "添加设备", new HH_Spinner.ResultCallBack() {
                    @Override
                    public void callback(int pos, String value) {
                        mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("%d-%s", pos, value));
                        mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                        if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                            list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {
                        HH_Logging.i(TAG, "ResultDisBack2");
                    }
                });
                break;
            }
            case R.id.button_popwindow3:{
                List<HH_T_SpinnerRosInfo> mListType = new ArrayList<>();
                mListType.add(new HH_T_SpinnerRosInfo(getDrawable(R.drawable.dt1),getDrawable(R.drawable.dt11), "霓虹影", false));
                mListType.add(new HH_T_SpinnerRosInfo(getDrawable(R.drawable.dt2),getDrawable(R.drawable.dt21), "点溪沙", false));
                mListType.add(new HH_T_SpinnerRosInfo(getDrawable(R.drawable.dt3),getDrawable(R.drawable.dt31), "星空织", false));
                for (int i = 0; i < mListType.size(); i++){
                    if (i == popWindowSel)
                        mListType.get(i).setSelect(true);
                }

                XTUI.Spinner3(systemParams, mListType, button_popwindow3, button_popwindow3.getWidth(), 500, popWindowSel, new HH_Spinner.ResultCallBack3() {
                    @Override
                    public void callback(int pos, HH_T_SpinnerRosInfo value) {
                        popWindowSel = pos;
                        mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("%d-%s", pos, value));
                        mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                        if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                            list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    }
                }, new HH_Spinner.ResultDisBack() {
                    @Override
                    public void callback(int pos, String value) {
                        HH_Logging.i(TAG, "ResultDisBack2");
                    }
                });
                break;
            }
            case R.id.button_nfc_unlock:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = XTCore.NFCUnLock(systemParams, testMac, strDeviceIdentity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_ver_lib:{
                mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("AAR版本号：%d", XTCore.GetLibVer()));
                mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                    list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                break;
            }
            case R.id.button_ver_hard:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_Logging.i(TAG, systemParams,"固件版本号：%s", XTCore.GetHardVersion(systemParams, testMac));
                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("固件版本号：%s", XTCore.GetHardVersion(systemParams, testMac)));
                            mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                            if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                                list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_DEL:{
                String s = textView_mac.getText().toString();
                if (s == null)
                    break;
                int len = s.length();
                if (len > 0)
                    s = s.substring(0, len - 1);
                textView_mac.setText(s);
                break;
            }
            case R.id.button_DELALL:{
                textView_mac.setText("");
                break;
            }
            case R.id.button_0:{
                InputNumber("0");
                break;
            }
            case R.id.button_1:{
                InputNumber("1");
                break;
            }
            case R.id.button_2:{
                InputNumber("2");
                break;
            }
            case R.id.button_3:{
                InputNumber("3");
                break;
            }
            case R.id.button_4:{
                InputNumber("4");
                break;
            }
            case R.id.button_5:{
                InputNumber("5");
                break;
            }
            case R.id.button_6:{
                InputNumber("6");
                break;
            }
            case R.id.button_7:{
                InputNumber("7");
                break;
            }
            case R.id.button_8:{
                InputNumber("8");
                break;
            }
            case R.id.button_9:{
                InputNumber("9");
                break;
            }
            case R.id.button_A:{
                InputNumber("A");
                break;
            }
            case R.id.button_B:{
                InputNumber("B");
                break;
            }
            case R.id.button_C:{
                InputNumber("C");
                break;
            }
            case R.id.button_D:{
                InputNumber("D");
                break;
            }
            case R.id.button_E:{
                InputNumber("E");
                break;
            }
            case R.id.button_F:{
                InputNumber("F");
                break;
            }
            case R.id.button_scan:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_Logging.i(TAG, systemParams, "scan-begin");
                            //ArrayList<HH_T_ScanResult> list = XTCore.Scan(systemParams, "");
                            ArrayList<HH_T_ScanResult> list = XTCore.Scan(systemParams, testMac);
                            if (list != null){
                                for (int i = 0; i < list.size(); i++){
                                    if (list.get(i).getBindDevice() != null){
                                        HH_Logging.i(TAG, systemParams, "mac=%s,rssi=%d,width=%d,height=%d",
                                                list.get(i).getBindDevice().getMacRes(), list.get(i).getBindDevice().getDbm(),
                                                list.get(i).getBindDevice().getWidth(), list.get(i).getBindDevice().getHeight());

                                        deviceW = list.get(i).getBindDevice().getWidth();
                                        deviceH = list.get(i).getBindDevice().getHeight();
                                        deviceColorType = list.get(i).getBindDevice().getDeviceColorType();

                                        HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
                                        HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
                                        HH_INI.putString(systemParams, "DEVICE_MAC", list.get(i).getBindDevice().getMacRes());
                                        HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);
                                    }
                                }
                            }
                            HH_Logging.i(TAG, systemParams, "scan-end");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_brushpic_cancel:{
                XTCore.StopBrushPic();
                break;
            }
            case R.id.button_brushpic_1:{
                if (listSendData1== null || listSendData1.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = XTCore.BrushPic(systemParams,
                                    testMac, listSendData1, nPageIntervalTime,
                                    true, strDeviceIdentity, 2000,
                                    epdParams);


                            /*List<Integer> list = new ArrayList<>();
                            list.add(0x1);
                            list.add(0x2);
                            list.add(0x3);
                            list.add(0x4);
                            list.add(0x5); // 长度
                            byte []prism = HH_CharUtil.ListToByte(list);
                            HH_EnumErrorCode code = XTCore.BrushPrism(systemParams,
                                    testMac, listSendData1, 5,
                                    false, "", 1, prism, prism.length);

                            HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());*/

                            // 如果刷失败了
                            //HH_SystemUtil.Sleep(2000);
                            //code = XTCore.NFCClose(systemParams, testMac, strDeviceIdentity);
                            //HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());

                        } catch (Exception e) {
                            HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }

            case R.id.button_del_all_pic:{
                // 这里也会传一张图
                if (listSendData2== null || listSendData2.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = XTCore.ImageStorage(systemParams, testMac, listSendData2,
                                    nPageIntervalTime, strDeviceIdentity, HH_EnumPicStorage_PicIndex.DEL_ALL, HH_EnumPicStorage_BrushType.SAVE_NO_BRUSH, 0, null);
                            HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                        } catch (Exception e) {
                            HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_brushpic_2:{
                if (listSendData2== null || listSendData2.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = HH_BleCommunicator.BrushPic(systemParams,
                                    testMac,
                                    listSendData2,
                                    nPageIntervalTime,
                                    true,
                                    strDeviceIdentity,
                                    2000,
                                    epdParams);
                            HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                            //HH_SystemUtil.Sleep(2000);
                            //code = XTCore.NFCClose(systemParams, testMac, strDeviceIdentity);
                            HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                        } catch (Exception e) {
                            HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_brushpic_3:{
                if (listSendData3== null || listSendData3.size() <= 0){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "先选图");
                    break;
                }
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = HH_BleCommunicator.BrushPic(systemParams,
                                    testMac,
                                    listSendData3,
                                    nPageIntervalTime,
                                    true,
                                    strDeviceIdentity,
                                    2000,
                                    epdParams);
                            HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                            //HH_SystemUtil.Sleep(2000);
                            //code = XTCore.NFCClose(systemParams, testMac, strDeviceIdentity);
                            //HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                        } catch (Exception e) {
                            HH_Logging.i(TAG, systemParams, "brush-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.imageview_pic_1:
            case R.id.imageview_pic_2:
            case R.id.imageview_pic_3:{
                Intent intent = new Intent(this, HH_Activity_PicFileSelect.class);
                ArrayList<String> listExts = new ArrayList<String>();
                listExts.add("png");
                listExts.add("jpg");
                listExts.add("jpeg");
                listExts.add("bmp");
                intent.putStringArrayListExtra(HH_Activity_PicFileSelect.FIND_FILE_EXT, listExts);
                intent.putExtra(HH_Activity_PicFileSelect.GET_FOLDER_DETAILS_INFO, false);
                startActivityForResult(intent, HH_MSG.XT_FILE_SELECT_PIC);
                break;
            }
            case R.id.button_nfc_close:{
                if (strDeviceIdentity == null || strDeviceIdentity.length() != 10){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "身份验证信息错误");
                    return;
                }
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = XTCore.NFCClose(systemParams, testMac, strDeviceIdentity);
                            HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_ota:{
                Intent intent = new Intent(this, HH_Activity_PicFileSelect.class);
                ArrayList<String> listExts = new ArrayList<String>();
                listExts.add("bin");
                intent.putStringArrayListExtra("FIND_FILE_EXT", listExts);
                intent.putExtra(HH_Activity_PicFileSelect.GET_FOLDER_DETAILS_INFO, false);
                startActivityForResult(intent, HH_MSG.XT_FILE_SELECT_OTA);
                break;
            }
            case R.id.button_identity:{
                if (strDeviceIdentity == null || strDeviceIdentity.length() != 10){
                    HH_ToastUtils.showToast(systemParams.getCtx(), "身份验证信息错误");
                    break;
                }
                // 安全模式，身份认证 打开
                HH_DialogUtil.showDialog(this, "警告，添加身份认证，可能会使设备无法使用", new HH_DialogUtil.ResultCallBack() {
                    @Override
                    public void callback(boolean flag) {
                        if (flag){
                            new Thread(new Runnable() {
                                public void run() {
                                    try {
                                        HH_EnumErrorCode result = XTCore.IdentityEncryption(systemParams, testMac, strDeviceIdentity, true);
                                        HH_Logging.i(TAG, systemParams, "XTCore.IdentityEncryption=%s", result.toString());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).start();
                        }
                    }
                });
                break;
            }
            case R.id.button_close_identity:{
                // 安全模式，身份认证 关闭
                HH_DialogUtil.showDialog(this, "警告，此操作将关闭身份认证", new HH_DialogUtil.ResultCallBack() {
                    @Override
                    public void callback(boolean flag) {
                        if (flag){
                            new Thread(new Runnable() {
                                public void run() {
                                    try {
                                        HH_EnumErrorCode result = XTCore.IdentityEncryption(systemParams, testMac, strDeviceIdentity, false);
                                        HH_Logging.i(TAG, systemParams, "XTCore.IdentityEncryption=%s", result.toString());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).start();
                        }
                    }
                });
                break;
            }

        }
    }


    public void InputNumber(String number){
        if (number == null)
            return;

        if (number.length() == 0){
            return;
        }

        String s1 = textView_mac.getText().toString();
        if (s1 == null)
            return;
        if (s1.length() >= 12)
            return;
        s1 = s1 + number;
        textView_mac.setText(s1);


        if (s1 != null && s1.length() == 12){
            testMac = s1;
            HH_INI.putString(systemParams, "DEVICE_MAC", testMac);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
           if (requestCode == HH_MSG.XT_FILE_SELECT_PIC){
                if (data != null) {
                    String path = data.getStringExtra("IMPORT_PATH_FILE");
                    /*
                    Bitmap b =  HH_PicUtils.zoomImg2R(HH_PicUtils.GetBitmapFormFile(path), 512,512,255, 255, 255);
                    String tempPath1 = HH_SystemUtil.GetTempPath(systemParams,10, HH_EnumFileType.BMP);
                    HH_PicUtils.BitmapSaveFile(b, tempPath1);
                    */

                    LoadLocalPic(path);
                }
           } else if (requestCode == HH_MSG.XT_FILE_SELECT_OTA){
                if (data != null) {
                    String path = data.getStringExtra("IMPORT_PATH_FILE");
                    OTA(path);
                }
            }
        }
    }

    private void OTA(String path) {
        if (strDeviceIdentity == null || strDeviceIdentity.length() != 10){
            HH_ToastUtils.showToast(systemParams.getCtx(), "身份验证信息错误");
            return;
        }

        new Thread(new Runnable() {
            public void run() {
                try {
                    HH_EnumErrorCode code = XTCore.OTA(systemParams, testMac, path, nPageIntervalTime, strDeviceIdentity);
                    HH_Logging.i(TAG, systemParams, "XTCore.OTA=%s", code.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    // 监听各种状态
    public class HandlenRecvMessage extends Handler {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            HH_Logging.i(TAG, "what = %d", msg.what);

            switch (msg.what){
                /*case XTCore.MSG_SPININFO:{
                    mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("%d-%s", msg.arg1, msg.obj));
                    mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                    if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                        list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    break;
                }*/
                case XTCore.MSG_PROGRESS:{
                    progressba_brushpic.setMax(msg.arg2);
                    progressba_brushpic.setProgress(msg.arg1 + 1);

                    // 进度值从0开始
                    mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("数据传输进度%d/%d,大包间隔=%s", msg.arg1, msg.arg2, msg.obj));
                    mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                    if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                        list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
                    break;
                }
                case XTCore.MSG_LOG:{
                    HH_EnumErrorCode errorCode = HH_EnumErrorCode.values()[msg.arg2];
                    switch (errorCode) {
                        case DATA_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("数据错误");
                            break;
                        case BLE_CONNECT_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("蓝牙连接失败");
                            break;
                        case BLE_CHARACTERISTIC_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("特征获取失败");
                            break;
                        case CLEAR_CACHE_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("发送清空缓存指令失败");
                            break;
                        case SCAN_FINISH:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("扫描完成");
                            break;
                        case BLE_BEGIN_CONNECT:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("蓝牙开始连接");
                            break;
                        case BLE_DISABLE:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("蓝牙没有打开");
                            break;
                        case BLE_BRUSH_BEGIN:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("刷图开始");
                            break;
                        case BLE_BRUSH_DATA_SEND:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("数据传输中");
                            break;
                        case BLE_NFC_CLOSE_BEGIN:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("NFC断电开始");
                            break;
                        case BLE_OTA_BEGIN:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("OTA开始");
                            break;
                        case SCAN_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("扫描失败");
                            break;
                        case SCAN_BEGIN:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("扫描开始");
                            break;
                        case SCAN_INFO: {
                            HH_T_BindDevice tbd = (HH_T_BindDevice) msg.obj;
                            if (tbd != null)
                                mAdapterCurrentTaskLogInfo.addTaskInfo(tbd.getMacRes());
                            break;
                        }
                        case SUCCESS:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("成功");
                            break;
                        case FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("失败");
                            break;
                        case BLE_CONNECT_SUCCESS:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("蓝牙连接成功");
                            break;
                        case BLE_BRUSH_ING:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("正在刷图");
                            break;
                        case BLE_DISCONNECT:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("蓝牙已断开");
                            break;
                        case BLE_IDENTITY_ENCRYPTION_BEGIN:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("身份加密开始");
                            break;
                        case BLE_IDENTITY_ENCRYPTION_FAIL:
                            mAdapterCurrentTaskLogInfo.addTaskInfo("认证失败");
                            break;
                        case TIME_CONSUME_BLE_CONNECT:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("连接用时：%d", msg.arg1));
                            break;
                        }
                        case TIME_CONSUME_DATA_SEND:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("数据发送用时：%d", msg.arg1));
                            break;
                        }
                        case TIME_CONSUME_BRUSH:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("刷图用时：%d", msg.arg1));
                            break;
                        }
                        case OTHER_INFO:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo((String)msg.obj);
                            break;
                        }
                    }


                    mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                    if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                        list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);


                    break;
                }
                case MSG_NFC_STATE:{
                    HH_EnumNfcErrorCode errorCode = HH_EnumNfcErrorCode.values()[msg.arg2];
                    switch (errorCode){
                        case BIND_INFO:     {
                            BleNFCBindInfo bindInfo = (BleNFCBindInfo)msg.obj;
                            if (bindInfo == null)
                                break;

                            HH_Logging.i(TAG, "NFC_绑定信息=%s,%d,%d,%s,%d",
                                    bindInfo.mac,
                                    bindInfo.deviceWidth,
                                    bindInfo.deviceHeight,
                                    bindInfo.deviceColor,
                                    bindInfo.deviceNumber);

                            deviceW = bindInfo.deviceWidth;
                            deviceH = bindInfo.deviceHeight;
                            deviceColorType = HH_SystemUtil.MadeColorType(bindInfo.deviceColor);
                            testMac = bindInfo.mac;

                            HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
                            HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
                            HH_INI.putString(systemParams, "DEVICE_MAC", bindInfo.mac);
                            HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);

                            textView_w.setText(String.format("%d", deviceW));
                            textView_h.setText(String.format("%d", deviceH));
                            textView_mac.setText(testMac);
                            textView_color.setText(String.format("%d", deviceColorType.ordinal()));

                            if (bindInfo.deviceNumber == 113){
                                if (bindInfo.customSN != null && bindInfo.customSN.length() == 17){
                                    HH_T_SNInfo snInfo = XTCore.GetTagSNInfo(bindInfo.customSN);
                                    mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("SN：项目代码=%s;手机型号=%s;款式类型=%s;年=%s;月=%s;日=%s;产品流水号=%s",
                                            snInfo.getProjectCode(),
                                            snInfo.getPhoneModel(),
                                            snInfo.getStyleType(),
                                            snInfo.getYear(),
                                            snInfo.getMonth(),
                                            snInfo.getDay(),
                                            snInfo.getSerialNumber()));
                                }
                            }

                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("新绑定：%s,%d,%d,%d", testMac, deviceW, deviceH, deviceColorType.ordinal()));
                            break;
                        }
                        case INCOMPATIBLE_TAG:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("不兼容标签");
                            break;
                        }
                        case ABNORMAL_TAG:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("无效标签");
                            break;
                        }
                        case DISABLE:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("NFC 不存在或未打开");
                            break;
                        }
                        case ABNORMAL_DATA:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("数据异常");
                            break;
                        }
                        case ABNORMAL_COLOR:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("颜色异常");
                            break;
                        }
                        case ABNORMAL_PARAMS:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("参数异常");
                            break;
                        }
                        case ABNORMAL_PIC_PATH:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("图片不存在");
                            break;
                        }
                        case ESL_HINT_ERROR:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("请移动设备以找到合适的NFC接触区域");
                            break;
                        }
                        case ESL_SUCCESS:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("刷图成功");
                            break;
                        }
                        case BRUSH:{
                            mAdapterCurrentTaskLogInfo.addTaskInfo("刷图中");
                            break;
                        }
                    }


                    mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                    if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                        list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);

                    break;
                }
            }
        }
    }






    /*public static DeviceInfo getDeviceInfo() {
        return deviceInfo;
    }

    public static void setDeviceInfo(DeviceInfo deviceInfo) {
        MainActivity.deviceInfo = deviceInfo;
    }

    public static Handler getHandler() {
        return mHandler;
    }

    public static void setHandler(Handler mHandler) {
        MainActivity.mHandler = mHandler;
    }*/



    /*public static class ReceiveHandler extends Handler {
        WeakReference<MainActivity> reference;

        public ReceiveHandler(MainActivity activity) {
            reference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity activity = (MainActivity) reference.get();
            if (null != activity) {
                activity.dismissNfcDialog();
                switch (msg.what) {
                    case 0:
                        String deviceInfo = (String) msg.obj;
                        //LogUtil.d(deviceInfo);
                        //activity.loadDeviceInfo(deviceInfo);
                        break;
                    default:
                        if(msg.obj == null || msg.obj == ""){
                            //HintDialog.faileDialog(activity.mContext, UIUtils.getString(activity.mContext, R.string.text_error));
                        }else {
                            //HintDialog.faileDialog(activity.mContext, (String) msg.obj);
                        }
                        break;
                }
            }
        }

    }*/
}